package com.aaronprj.school.mbooks.resource;

import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.aaronprj.common.entities.SgmenuEntityManager;
import com.aaronprj.common.entities.mbooks.BookItem;
import com.aaronprj.common.entities.mbooks.BookOrder;
import com.aaronprj.common.entities.mbooks.OrderItem;
import com.aaronprj.common.enums.StatusType;
import com.aaronprj.common.web.uivo.BaseEntity;
import com.aaronprj.school.mbooks.uivo.UIBookOrder;
import com.aaronprj.school.mbooks.uivo.UIOrderItem;
import com.aaronprj.school.mbooks.uivo.UIOrderObj;

@Path("/order")
public class OrderResource extends MbooksResource{
	
	
	@Path("/placeorder")
    @POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    public String placeOrder(@QueryParam("sessionId") String sessionId, 
			 				 @QueryParam("customerId") int customerId,
    						 @QueryParam("customerName") String customerName, 
    						 @QueryParam("phone") String phone, 
    						 @QueryParam("email") String email,
    						 @QueryParam("shippingAdd") String shippingAdd, 
    						 @QueryParam("postCode") String postCode, 
    						 @QueryParam("subtotal") double subtotal, 
    						 @QueryParam("serviceCharge") double serviceCharge, 
    						 @QueryParam("totalAmount") double totalAmount){
		
		
		
		BookOrder po = new BookOrder();
		po.setCreateDate(new Date());
		po.setCustomerId(customerId);
		po.setCustomerName(customerName);
		po.setOrderNo("NO"+customerId+"00"+po.getCreateDate().getTime());
		po.setPhone(phone);
		po.setEmail(email);
		po.setShippingAddress(shippingAdd);
		po.setPostCode(postCode);
		
		po.setShippingDate(new Date());
		po.setServiceCharge(serviceCharge);
		po.setStatus(StatusType.OPEN);
		po.setSubtotal(subtotal);
		po.setTotalAmount(totalAmount);
			
		SgmenuEntityManager.save(po, true);
		
    	return this.writeValueAsString(generateResult(BaseEntity.class, true, "Your order  success", "Your order NO:"+po.getOrderNo(), sessionId, po.getOrderNo()));
	}


	@Path("/orderitem/{orderno}/{itemid}/{units}/{price}")
    @POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    public String addOrderItem(@PathParam("orderno") String orderno, 
    						   @PathParam("itemid") int itemid, 
    						   @PathParam("units") int units, 
    						   @PathParam("price") double price){
		
		
		OrderItem oi = new OrderItem();
		BookItem fi = SgmenuEntityManager.find(BookItem.class, itemid);
		BookOrder po = (BookOrder) SgmenuEntityManager.getItem("BookOrder.findByOrderNo", "orderNo", orderno);
		oi.setBookOrder(po);
		oi.setItem(fi);
		oi.setUnits(units);
		oi.setTotalPrice(price);

		SgmenuEntityManager.save(oi, true);
		
    	return this.writeValueAsString(generateResult(BaseEntity.class, true, "Your order item added success", "Your order NO:"+orderno));
	}

	@Path("/orders/{customerid}")
    @GET
	@Produces(MediaType.APPLICATION_JSON)
	public String getOrders(@PathParam("customerid") int customerId){
		
		UIOrderObj uiorder = generateResult(UIOrderObj.class, true, "", "");
		
		List<BookOrder> orders = (List<BookOrder>) SgmenuEntityManager.getItems("BookOrder.findByCustomer", "customerId", customerId);

		if(null == orders || orders.isEmpty()){
			return this.writeValueAsString(generateResult(UIOrderObj.class, true, "No record found", ""));
		}
		
		for(BookOrder bo:orders){
			uiorder.addUIBookOrder(this.buildUIBookOrder(bo));
		}
	
    	return writeValueAsString(uiorder);
		
	}
	
	@Path("/orders")
    @GET
	@Produces(MediaType.APPLICATION_JSON)
	public String getOrders(){
		
		UIOrderObj uiorder = generateResult(UIOrderObj.class, true, "", "");
		
		List<BookOrder> orders = (List<BookOrder>) SgmenuEntityManager.getAllItems("BookOrder");

		if(null == orders || orders.isEmpty()){
			return this.writeValueAsString(generateResult(UIOrderObj.class, true, "No record found", ""));
		}
		
		for(BookOrder bo:orders){
			uiorder.addUIBookOrder(this.buildUIBookOrder(bo));
		}
	
    	return writeValueAsString(uiorder);
		
	}
	
	private UIBookOrder buildUIBookOrder(BookOrder bo){
		UIBookOrder uibo = new UIBookOrder();

		uibo.setId(bo.getId());
		uibo.setOrderNo(bo.getOrderNo());
		uibo.setCustomerId(bo.getCustomerId());
		uibo.setCustomerName(bo.getCustomerName());
		
		uibo.setStatus(bo.getStatus());
		uibo.setShippingAddress(bo.getShippingAddress());
		uibo.setPostCode(bo.getPostCode());
		uibo.setPhone(bo.getPhone());
		uibo.setEmail(bo.getEmail());
		uibo.setShippingDate(bo.getShippingDate().toString());
		uibo.setCreateDate(bo.getCreateDate().toString());
		
		uibo.setSubtotal(bo.getSubtotal());
		uibo.setServiceCharge(bo.getServiceCharge());
		uibo.setTotalAmount(bo.getTotalAmount());
		
		for(OrderItem oi:bo.getOrderItems()){
			UIOrderItem uo = new UIOrderItem();
			uo.setId(oi.getId());
			uo.setItemId(oi.getItem().getId());
			uo.setItemName(oi.getItem().getName());
			uo.setTotalPrice(oi.getTotalPrice());
			uo.setUnits(oi.getUnits());
			
			uibo.addUIOrderItem(uo);
		}
		
		
		return uibo;
	}
	
}
